import getpath
import os
import logging
import csv
import myglobal
import config_json
import sample
import openpyxl
dict930= {
        "fields": [
            {
                "name": "sampleId",
                "title": "\u6837\u54c1\u540d\u79f0",
                "at": 1
            },
            {
                "name": "time",
                "title": "\u65e5\u671f",
                "at": 14
            },
            {
                "name": "O",
                "title": "O%(m/m)",
                "at": 7
            },
            {
                "name": "N",
                "title": "N%(m/m)",
                "at": 8
            },
            {
                "name": "H",
                "title": "H%(m/m)",
                "at": 9
            },
            {
                "name": "m",
                "title": "\u8d28\u91cf(g)",
                "at": 2
            }
        ],
    }
def in_list(one, l1):
    for i in range(len(l1)):
        logging.info(str(one)+str(l1[i]))
        if one in l1[i]:
            return i
    return -1
def csv_data(fname):
    logging.info(fname)
    file=open(fname, encoding="gbk")
    csv_reader = csv.reader(file)
    data = []
    for row in csv_reader:
        data.append(row)
    file.close()
    return data
def getdata(fname):
    r=[]
    data = csv_data(fname)
    title = data[0]
    data = data[1:]
    logging.info(title)
    # logging.info(data)
    for f in dict930["fields"]:
        f["at"] = in_list(f["title"], title)
    for one in data:
        logging.info(one)
        if len(one) < 2:
            continue
        dict1={}
        for f in dict930["fields"]:
            if f["at"] != -1:
                dict1[f["name"]]=one[f["at"]]
        sample={"time":dict1.get("time"),"mass":dict1.get("m"), "volume":"", "sampleid":dict1.get("sampleId").strip(), "conc": [], "items": []}
        for one in dict1.keys():
            if one in ["sampleId","m","time"]:
                pass
            else:
                sample["items"].append((one,""))
                sample["conc"].append(dict1.get(one))
        r.append(sample)
    return r
def test_getdata():
    fname=os.path.join(getpath.getpath(),"..","data","930.csv")
    data=getdata(fname)
    print(data)
if __name__=="__main__":
    test_getdata()